package com.gsit.expenditure.domain;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "vendor")
@DiscriminatorValue("VA")
@NamedQueries({@NamedQuery (name= "Vendor.findAll",query= "select vendor from Vendor vendor")})
public class Vendor extends BaseEntity {

	@Pattern  (regexp="(^[a-zA-Z]{0,15})", message="Enter a-z only")    
	@NotNull(message="First Name is Required")
	@NotEmpty(message="First Name is Required")
	@Column(name = "firstName")
	private String firstName;

	@Pattern  (regexp="(^[a-zA-Z]{0,15})", message="Enter a-z only")    
	@NotNull(message="Last Name is Required")
	@NotEmpty(message="Last Name is Required")
	@Column(name = "lastName")
	private String lastName;

	@Valid
	@OneToOne(optional = true ,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "address_id", insertable=true)
	private VendorAddress address;

	@Pattern  (regexp="(^[0-9+-]{0,15})",message="Invalid characters,Enter only (0-9,+,-)") 
	@Column(name = "phone")
	private String phone;

	@NotNull(message="Mobile Required")
	@NotEmpty(message="Mobile Required")
	@Pattern  (regexp="(^[0-9]{10})", message="Enter 10 digit number only")    
	@Column(name = "mobile")
	private String mobile;

	@OneToMany( fetch = FetchType.EAGER , cascade = CascadeType.ALL)
	private Set<Vehicle> vehicleSet;

	
	public String getFullName() {
		StringBuilder fullName = new StringBuilder();
		Boolean isFirst = Boolean.TRUE;
		if (firstName != null && !firstName.trim().equals("")) {
			fullName.append(firstName);
			isFirst = Boolean.FALSE;
		}

		if (lastName != null && !lastName.trim().equals("")) {
			if (!isFirst)
				fullName.append(" ");
			fullName.append(lastName);

		}

		return fullName.toString();

	}


	public String getFirstName() {
		return firstName;
	}


	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}


	public String getLastName() {
		return lastName;
	}


	public void setLastName(String lastName) {
		this.lastName = lastName;
	}


	public VendorAddress getAddress() {
		return address;
	}


	public void setAddress(VendorAddress address) {
		this.address = address;
	}


	public String getPhone() {
		return phone;
	}


	public void setPhone(String phone) {
		this.phone = phone;
	}


	public String getMobile() {
		return mobile;
	}


	public void setMobile(String mobile) {
		this.mobile = mobile;
	}


	public Set<Vehicle> getVehicleSet() {
		return vehicleSet;
	}


	public void setVehicleSet(Set<Vehicle> vehicleSet) {
		this.vehicleSet = vehicleSet;
	}
	
	
}
